﻿using DG.Tweening;
using UnityEngine;
using System.Collections;
using System;
using UnityEngine.UI;
using Code.CharacterSystem;

namespace Code.UI
{
    public class GameUI:MonoBehaviour
    {
        public static GameUI Instance=null;

        [SerializeField] private GameObject _playerInfo;
        [SerializeField] private GameObject _characterInfo;
        [SerializeField] private GameObject _mainPanel;

        private Vector3 _tempPlayerInfoPos;
        private Vector3 _tempCharacterInfoPos;
        private Vector3 _tempMainPanelPos;

        private bool _stopping;

        private UIPlayer _currentPlayer;

        [SerializeField] private Image _playerIcon;
        [SerializeField] private HealthBar _healthBar;
        [SerializeField] private HealthBar _mpBar;

        [SerializeField] private Transform _skillInfoTransform;
        [SerializeField] private GameObject _skillItem;

        private void Awake()
        {
            Instance = this;
        }
        private void Start()
        {
            _currentPlayer = GameObject.FindObjectOfType<UIPlayer>();
            InitGameUI();
            PlayerInfoAnim();
        }


        private void InitGameUI()
        {
            _playerIcon.sprite = _currentPlayer.Config.Icon;
            _healthBar.SetMaxHealth(_currentPlayer.Config.MaxHp);
            _mpBar.SetMaxHealth(_currentPlayer.Config.MaxMp);
            foreach (var skill in _currentPlayer.SkillList)
            {
                var item=GameObject.Instantiate(_skillItem,_skillInfoTransform);
                if(item.TryGetComponent<SkillItem>(out var skillItem)) 
                {
                    skillItem.InitSkillItem(_currentPlayer,skill);
                }
            }
        }

        private void PlayerInfoAnim()
        {
            _tempPlayerInfoPos = _playerInfo.transform.position;
            _tempCharacterInfoPos = _characterInfo.transform.position;
            _tempMainPanelPos = _mainPanel.transform.position;
            Sequence mySequence = DOTween.Sequence();

            var playerInfoPosTweener = DOTween.To(() => _tempPlayerInfoPos, x => _tempPlayerInfoPos = x, new Vector3(_tempPlayerInfoPos.x, _tempPlayerInfoPos.y + 400, _tempPlayerInfoPos.z), 1);
            var characterInfoPosTweener = DOTween.To(() => _tempCharacterInfoPos, x => _tempCharacterInfoPos = x, new Vector3(_tempCharacterInfoPos.x - 200, _tempCharacterInfoPos.y, _tempCharacterInfoPos.z), 1);
            var mainPanelPosTweener = DOTween.To(() => _tempMainPanelPos, x => _tempMainPanelPos = x, new Vector3(_tempMainPanelPos.x + 1600, _tempMainPanelPos.y, _tempMainPanelPos.z), 1);

            mySequence.Append(playerInfoPosTweener);
            mySequence.Join(characterInfoPosTweener);
            mySequence.Join(mainPanelPosTweener);

            mySequence.OnComplete(() => { _stopping = true; });
            StartCoroutine(InfoMoveCoro());
        }

        private IEnumerator InfoMoveCoro()
        {
            while (true) 
            {
                _playerInfo.transform.position = _tempPlayerInfoPos;
                _characterInfo.transform.position = _tempCharacterInfoPos;
                _mainPanel.transform.position = _tempMainPanelPos;
                if (_stopping) 
                {
                    break;
                }
                yield return null;
            }
        }

      
    }
}
